package com.isyscore.iios.appdemo.controller;


import com.google.common.collect.Lists;
import com.isyscore.iios.appdemo.dto.AclDTO;
import com.isyscore.iios.appdemo.dto.AclRegisterBodyDTO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * 该案例演示了 <权限管理>应用如何接入应用的功能权限（菜单）的实际案例
 * 相关介绍，参考：https://support.isyscore.com/document/detail?pageId=5795&bookId=26
 */
@RestController
@RequestMapping("${app.apiPrefix}/acl")
public class AppAclController {
    @Value("${app.appCode}")
    private String appCode;

    /**
     * 注意，该接口必须能够匿名访问
     * 接口返回格式如下：
     * [{
     * 	"appCode": "iios-app-demo",
     * 	"acls": [
     *                {
     * 			"code": "sale",
     * 			"name": "销售管理",
     * 			"url": "",
     * 			"type": 1,
     * 			"seq": 1,
     * 			"status": 1,
     * 			"remark": "",
     * 			"aclModuleList": [
     *                {
     * 					"code": "customer",
     * 					"name": "客户管理",
     * 					"url": "",
     * 					"type": 1,
     * 					"seq": 1,
     * 					"status": 1,
     * 					"remark": "",
     * 					"aclModuleList": [],
     * 					"aclList": [
     *                        {
     * 							"code": "customer-add",
     * 							"name": "新增客户",
     * 							"url": "",
     * 							"type": 1,
     * 							"seq": 1,
     * 							"status": 1,
     * 							"remark": ""
     *                        },
     *                        {
     * 							"code": "customer-del",
     * 							"name": "删除客户",
     * 							"url": "",
     * 							"type": 1,
     * 							"seq": 2,
     * 							"status": 1,
     * 							"remark": ""
     *                        }
     * 					]
     *                },
     *                {
     * 					"code": "order",
     * 					"name": "订单管理",
     * 					"url": "",
     * 					"type": 1,
     * 					"seq": 2,
     * 					"status": 1,
     * 					"remark": "",
     * 					"aclModuleList": [],
     * 					"aclList": [
     *                        {
     * 							"code": "order-add",
     * 							"name": "新增订单",
     * 							"url": "",
     * 							"type": 1,
     * 							"seq": 1,
     * 							"status": 1,
     * 							"remark": ""
     *                        },
     *                        {
     * 							"code": "order-del",
     * 							"name": "删除订单",
     * 							"url": "",
     * 							"type": 1,
     * 							"seq": 2,
     * 							"status": 1,
     * 							"remark": ""
     *                        }
     * 					]
     *                }
     * 			],
     * 			"aclList": []
     *        }
     * 	]
     * }]
     **/
    @PostMapping("/callback")
    public List<AclRegisterBodyDTO> getAcl() {
        AclRegisterBodyDTO body = new AclRegisterBodyDTO();
        //叶子节点新增客户
        AclDTO customerAdd = AclDTO.builder()
                .code("customer-add")
                .name("新增客户")
                .type(1)
                .seq(1)
                .remark("")
                .url("")
                .status(1)
                .build();
        //叶子节点删除客户
        AclDTO customerDel = AclDTO.builder()
                .code("customer-del")
                .name("删除客户")
                .type(1)
                .seq(2)
                .remark("")
                .url("")
                .status(1)
                .build();
        //非叶子节点客户管理
        AclDTO customer = AclDTO.builder()
                .code("customer")
                .name("客户管理")
                .type(1)
                .seq(1)
                .remark("")
                .url("")
                .status(1)
                .aclList(Lists.newArrayList(customerAdd, customerDel))
                .aclModuleList(Lists.newArrayList())
                .build();
        //叶子节点新增订单
        AclDTO orderAdd = AclDTO.builder()
                .code("order-add")
                .name("新增订单")
                .type(1)
                .seq(1)
                .remark("")
                .url("")
                .status(1)
                .build();
        //叶子节点删除订单
        AclDTO orderDel = AclDTO.builder()
                .code("order-del")
                .name("删除订单")
                .type(1)
                .seq(2)
                .remark("")
                .url("")
                .status(1)
                .build();
        //非叶子节点订单管理
        AclDTO order = AclDTO.builder()
                .code("order")
                .name("订单管理")
                .type(1)
                .seq(2)
                .remark("")
                .url("")
                .status(1)
                .aclList(Lists.newArrayList(orderAdd, orderDel))
                .aclModuleList(Lists.newArrayList())
                .build();
        //顶级节点销售管理
        AclDTO sale = AclDTO.builder()
                .code("sale")
                .name("销售管理")
                .type(1)
                .seq(1)
                .remark("")
                .url("")
                .status(1)
                .aclList(Lists.newArrayList())
                .aclModuleList(Lists.newArrayList(customer, order))
                .build();
        //组装最后对象
        body.setAppCode(appCode);
        body.setAcls(Collections.singletonList(sale));
        return Collections.singletonList(body);
    }

}
